package com.smart_campus.campus.controller;

import com.smart_campus.campus.domain.CampusCarousel;
import com.smart_campus.campus.domain.CampusMedia;
import com.smart_campus.campus.domain.NewsSchool;
import com.smart_campus.campus.service.NewsSchoolService;
import com.smart_campus.campus.vo.NewSchoolVo;
import com.smart_campus.sys.common.ActiveUser;
import com.smart_campus.sys.common.DataGridView;
import com.smart_campus.sys.common.ResultObj;
import com.smart_campus.sys.common.upload.UploadService;
import com.smart_campus.sys.domain.User;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.hibernate.validator.internal.util.privilegedactions.NewSchema;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

@RequestMapping("school")
@RestController
public class NewSchoolController {
    @Autowired
    UploadService uploadService;

    @Autowired
    private NewsSchoolService newsSchoolService;

    @RequestMapping("sel")
    public DataGridView sel(NewSchoolVo schoolVo){
        System.out.println("schoolVo = " + newsSchoolService.queryCarousel(schoolVo));
        return newsSchoolService.queryCarousel(schoolVo);

    }

    @RequestMapping("upd")
    public ResultObj upd(NewsSchool school,MultipartFile mf){
        try{
            String url = uploadGoodsFile(mf);
            System.out.println("url = " + url);
            school.setArtcleimg(url);
            newsSchoolService.updateByPrimaryKey(school);
            return ResultObj.News_UPDATE_YES;
        }catch (Exception e){
            e.printStackTrace();
            return ResultObj.News_UPDATE_NO;
        }
    }

    @RequestMapping("add")
    public ResultObj add(NewsSchool school, MultipartFile mf){
    try {
            String url = uploadGoodsFile(mf);
            System.out.println("url = " + url);
            school.setArtcleimg(url);
            newsSchoolService.insert(school);
            return ResultObj.News_INSERT_YES;
        }catch (Exception e){
            e.printStackTrace();
            return ResultObj.News_INSERT_NO;
        }
    }

    @RequestMapping("del")
    public ResultObj del(int id){
        try{
            newsSchoolService.deleteByPrimaryKey(id);
            return ResultObj.News_DELETE_YES;
        }catch (Exception e){
            e.printStackTrace();
            return ResultObj.News_DELETE_NO;
        }
    }

    @RequestMapping("delz")
    public ResultObj delz(int[] id){
        try{
            for (int i:id){
                newsSchoolService.deleteByPrimaryKey(i);
            }
            return ResultObj.News_DELETE_YES;
        }catch (Exception e){
            e.printStackTrace();
            return ResultObj.News_DELETE_NO;
        }
    }

    @RequestMapping("sellikez")
    public DataGridView sellikez(NewSchoolVo newSchoolVo){
        System.out.println("newSchoolVo = " + newSchoolVo);
        return newsSchoolService.selh(newSchoolVo);
    }


    public String uploadGoodsFile(MultipartFile mf){

        String path = this.uploadService.uploadImage(mf);

        Map<String,String> map=new HashMap<>();
        map.put("src",path);
        return path;
    }

    /**
     * 查询单个
     * @param newsSchool
     * @return
     */
    @RequestMapping("selectScholl")
    public NewsSchool selectJournalism(NewsSchool newsSchool){
        try {
            newsSchool=this.newsSchoolService.selectByPrimaryKey(newsSchool.getArticleid());
            System.out.println("校园要闻"+newsSchool);
            return newsSchool;
        } catch (Exception e) {
            e.printStackTrace();
            return newsSchool;
        }
    }


}
